package com.sikao.app.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.sikao.app.R;
import com.sikao.app.bean.CategoryAttrForInviteBean;
import com.sikao.app.ui.AddInviteTwoActivity;
import com.sikao.app.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 发布招标第二步 类型是3 单选的子项 有浮动值
 */
public class AddInviteStepTwoTypeThreeChildItemAdapter extends BaseAdapter  {
    CategoryAttrForInviteBean.ResBean resBean;//父级
    private List<CategoryAttrForInviteBean.ResBean.AttrValuesBean> objects = new ArrayList<CategoryAttrForInviteBean.ResBean.AttrValuesBean>();
    List<CategoryAttrForInviteBean.ResBean.DefaultValueBean> defaultValueBeanList=new ArrayList<>();//默认值
    private Context context;
    private LayoutInflater layoutInflater;
    int index=0;

    public AddInviteStepTwoTypeThreeChildItemAdapter(Context context, CategoryAttrForInviteBean.ResBean resBean, int index) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.resBean=resBean;
        objects=resBean.getAttr_values();
        defaultValueBeanList= resBean.getDefault_value();
        this.index=index;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public CategoryAttrForInviteBean.ResBean.AttrValuesBean getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         // if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.mll_add_step_prices_type_three_child_item, null);
            convertView.setTag(new ViewHolder(convertView));
         // }
        doSelected(position, convertView, parent);
        initializeViews(getItem(position), (ViewHolder) convertView.getTag(),position);
        return convertView;
    }


    private void doSelected(int position, View convertView, ViewGroup parent) {
        //默认值
        //Utils.showLog("====avb进入默认值"+AddInviteTwoActivity.initFlagTypeThree);
        if(!AddInviteTwoActivity.initFlagTypeThree) {

            CategoryAttrForInviteBean.ResBean.AttrValuesBean object = getItem(position);
            ViewHolder holder = (ViewHolder) convertView.getTag();
            for (int i = 0; i < AddInviteTwoActivity.resTypeThree.get(index).getDefault_value().size(); i++) {
                if (object.getId() == AddInviteTwoActivity.resTypeThree.get(index).getDefault_value().get(i).getId()) {
                    holder.cbThreeChild.setChecked(true);
                    holder.etThreeChildChangePrice.setEnabled(true);
                    holder.etThreeChildChangePrice.setClickable(true);
                    //修改单选数据组 属性值
                    AddInviteTwoActivity.resTypeThree.get(index).getAttr_values().get(position).setChecked(true);
                }

                if (parent != null) {
                    int size_ = parent.getChildCount();
                    //检查当前列表的个数，与数据的个数是否相等，即ListView已经初步加载完成
                    if (size_ > 0 && objects.size() == size_) {//至此，可以判定该处理接口已经成功执行过一次
                        Utils.showLog("====avb完成默认值 子总数量"+size_);
                        AddInviteTwoActivity.initFlagTypeThree = true;
                    }
                }
            }
        }
    }

    private void initializeViews(CategoryAttrForInviteBean.ResBean.AttrValuesBean object, final ViewHolder holder, final int position) {
        //TODO implement
        holder.tvThreeChildValue.setText(object.getValue());

        holder.cbThreeChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(((CheckBox)v).isChecked()){
                    holder.etThreeChildChangePrice.setEnabled(true);
                    holder.etThreeChildChangePrice.setClickable(true);
                    AddInviteTwoActivity.resTypeThree.get(index).getAttr_values().get(position).setChecked(true);
                }else{
                    holder.etThreeChildChangePrice.setEnabled(false);
                    holder.etThreeChildChangePrice.setClickable(false);
                    AddInviteTwoActivity.resTypeThree.get(index).getAttr_values().get(position).setChecked(false);
                }
            }
        });
        holder.etThreeChildChangePrice.setOnTouchListener(new View.OnTouchListener() {

            EditText et=null;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                et=(EditText)v;

                et.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        String changePrice = et.getText().toString();
                        AddInviteTwoActivity.resTypeThree.get(index).getAttr_values().get(position).setChangePrice(changePrice);
                    }
                });
                return  false;
            }
        });

    }



    protected class ViewHolder {
        private CheckBox cbThreeChild;
    private TextView tvThreeChildValue;
    private EditText etThreeChildChangePrice;

        public ViewHolder(View view) {
            cbThreeChild = (CheckBox) view.findViewById(R.id.cb_three_child);
            tvThreeChildValue = (TextView) view.findViewById(R.id.tv_three_child_value);
            etThreeChildChangePrice = (EditText) view.findViewById(R.id.et_three_child_change_price);
        }
    }
}
